<?php

/**
 * Description of Beanstalk_API
 *
 * @author XaeroDegreaz
 */
require_once("Account/Account.php");
require_once("Changeset/Changeset.php");
require_once("Release/Release.php");
require_once("Release_Server/Release_Server.php");
require_once("Repository/Repository.php");
require_once("User/User.php");

class Beanstalk_API {
    //put your code here
    public $domain = null;
    public $username = null;
    public $password = null;

    /**
     *	Setup the Beanstalk_API instance
     * @param <string> $domain
     * @param <string> $username
     * @param <string> $password
     */
    public function  __construct ($domain=null, $username=null, $password=null) {
	$this->domain = $domain;
	$this->username = $username;
	$this->password = $password;
    }

    protected function initialize($xml_file, $resources=null) {
	$url = $this->domain.$xml_file;
	//echo $url;
	$curl_handler = curl_init();
	curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl_handler, CURLOPT_URL, $url);
	curl_setopt($curl_handler, CURLOPT_USERPWD, $this->username.":".$this->password);


	if($resources) {
	    /*
	    * This isn't working yet. For some reason, there are no errors but there is no change
	    * on the Beanstalk account. It does however return a single space character in the result.
	    */
	    curl_setopt($curl_handler, CURLOPT_PUT, 4);
	    curl_setopt($curl_handler, CURLOPT_INFILE, $resources[0]);
	    curl_setopt($curl_handler, CURLOPT_INFILESIZE, strlen($resources[1]));
	    curl_setopt($curl_handler, CURLOPT_HTTPHEADER, array ('Expect: HEAD 200 OK'));

	    $result = curl_exec($curl_handler);
	}else {
	    $data = curl_exec($curl_handler);
	    //echo $data."<br/><br/>";

	    $result = new SimpleXMLElement($data);
	}

	curl_close($curl_handler);
	return $result;
    }

}
?>
